<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <el-container>
        <el-header style="background-color: #0095d7">

        </el-header>
        <el-container>
            <el-aside width="200px" style="overflow:visible">
                <!--侧边栏开始-->
                <el-menu
                        default-active="2"
                        class="el-menu-vertical-demo"
                        @select="handleSelect">
                    <el-submenu index="3">
                        <template slot="title">
                            <i class="el-icon-location"></i>
                            <span>商品管理</span>
                        </template>
                            <el-menu-item index="3-1">添加商品</el-menu-item>
                            <el-menu-item index="3-2">商品列表</el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
            <!--添加商品开始-->
                <el-form v-if="selectedIndex=='3-1'" :model="brandArr" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="商品名称">
                        <el-input v-model="brandArr.title"></el-input>
                    </el-form-item>
                    <el-form-item label="商品最新价格">
                        <el-input v-model="brandArr.price"></el-input>
                    </el-form-item>
                    <el-form-item label="商品旧价格">
                        <el-input v-model="brandArr.oldPrice"></el-input>
                    </el-form-item>
                    <el-form-item label="商品图片">
                        <el-upload
                                action="/upload"
                                name="picFile"
                                list-type="picture-card"
                                :on-preview="handlePictureCardPreview"
                                :on-remove="handleRemove"
                                :on-success="handleSuccess">
                            <i class="el-icon-plus"></i>
                        </el-upload>
                    </el-form-item>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm()">立即创建</el-button>
                    </el-form-item>
                </el-form>
            <!--添加商品结束-->
            <!--商品列表开始-->
                <el-table v-if="selectedIndex=='3-2'" :data="tableData" border style="width: 100%">
                    <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
                    <el-table-column prop="title" label="商品名称" align="center"></el-table-column>
                    <el-table-column prop="price" label="最新价格" width="150" align="center"></el-table-column>
                    <el-table-column prop="oldPrice" label="旧价格" width="150"  header-align="center"></el-table-column>
                    <el-table-column prop="url" label="图片路径" width="180" align="center"></el-table-column>
                    <el-table-column prop="sort" label="自定义排序序号" width="100" align="center"></el-table-column>
                    <el-table-column label="操作" width="200" align="center">
                        <template slot-scope="scope">
                            <el-button circle icon="el-icon-edit" size="mini" type="text" @click="handleEdit(scope.row.id)"></el-button>
                            <el-button circle icon="el-icon-delete" size="mini" type="danger" @click="openDeleteConfirm(scope.row.id)"></el-button>
                            <!--修改弹出页面-->
                            <el-dialog title="修改商品信息" :visible.sync="dialogFormVisible">
                                <el-form :model="brandArr">
                                    <el-form-item label="商品名称">
                                        <el-input v-model="brandArr.title"></el-input>
                                    </el-form-item>
                                    <el-form-item label="商品最新价格">
                                        <el-input v-model="brandArr.price"></el-input>
                                    </el-form-item>
                                    <el-form-item label="商品旧价格">
                                        <el-input v-model="brandArr.oldPrice"></el-input>
                                    </el-form-item>
                                    <el-form-item label="商品自定义排序序号">
                                        <el-input v-model="brandArr.sort"></el-input>
                                    </el-form-item>
                                    <el-form-item label="商品图片">
                                        <el-upload
                                                action="/upload"
                                                name="picFile"
                                                list-type="picture-card"
                                                :on-preview="handlePictureCardPreview"
                                                :on-remove="handleRemove"
                                                :on-success="handleSuccess">
                                            <i class="el-icon-plus"></i>
                                        </el-upload>
                                    </el-form-item>
                                    <el-dialog :visible.sync="dialogVisible">
                                        <img width="100%" :src="dialogImageUrl" alt="">
                                    </el-dialog>
                                </el-form>
                                <div slot="footer" class="dialog-footer">
                                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                                    <el-button type="primary" @click="Update(v.brandArr)">确 定</el-button>
                                </div>
                            </el-dialog>
                        </template>
                    </el-table-column>
                </el-table>
            <!--商品列表结束-->
        </el-main>
        </el-container>
    </el-container>
</div>


</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue@2.6.14/dist/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
    let v = new Vue({
        el: "#app",
        data: function (){
            return{
                dialogImageUrl: '',
                dialogVisible: false,
                selectedIndex:"",
                brandArr: {
                    id:'',
                    title : '',
                    price : '',
                    oldPrice : '',
                    url : '',
                    sort:''
                },
                rules: {

                },
                tableData:[
                    id='',
                    title='',
                    price='',
                    oldPrice='',
                    url='',
                    sort=''
                ],
                dialogFormVisible: false,
            }
        },
        methods: {
            handleSelect(index){
                console.log(index)
                if(index=="3-1"){
                    console.log("添加商品")
                    v.selectedIndex=index;
                    console.log(v.dialogImageUrl)
                }else if(index=="3-2"){
                    console.log("商品列表")
                    v.selectedIndex=index;
                    axios.get("/brands/list").then(function (response) {
                        v.tableData=response.data
                    })
                }
            },
            //图片
            handleRemove(file, fileList) {
                console.log(file, fileList);
                //发请求告诉服务器删除文件夹里面的文件
                //得到要删除的文件名
                let fileName= file.response;
                console.log("文件名:"+fileName);
                axios.get("/remove?fileName="+fileName).then(function (response) {
                    console.log("服务器文件删除完成!");
                })
            },
            handlePictureCardPreview(file) {
                let fileName= file.response;
                console.log("文件名:"+fileName);
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleSuccess(response, file, fileList){
                let fileName= file.response;
                v.brandArr.url=fileName
                console.log("图片路径:"+v.brandArr.url)
            },
            //添加表单
            submitForm() {
                console.log("结果是:"+v.brandArr)
                axios.post("/brands/add-new",v.brandArr).then(function (response) {

                })
                alert("添加成功")
                location.reload()
            },
            //修改列表数据
            handleEdit(id) {
                v.dialogFormVisible = true
                v.brandArr.id=id
                console.log('将编辑id=' + id + "的品牌数据……");
            },
            Update(brand){
                console.log(brand)
                axios.post("/brands/update",v.brandArr).then(function () {
                    console.log(brand)
                })
                alert("修改成功")
                location.reload()
            },
            //删除列表的某一项
            openDeleteConfirm(id) {
                this.$confirm('此操作将永久删除该品牌, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.handleDelete(id);
                }).catch(() => {
                });
            },
            handleDelete(id) {
                console.log('将删除id=' + id + "的品牌数据……");
                let url = '/brands/' + id + '/delete';
                axios.post(url).then((response) => {
                    location.reload();
                    axios.get("/brands/list").then(function (response) {
                        v.tableData=response.data
                    })
                })
            }
        }
    })
</script>
</html>